﻿using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Windows.Interactivity;
using nRoute.Navigation.Containers;
using nRoute.SiteMaps;

namespace nRoute.Samples.Netflix.Behaviors
{
    /// <remarks>Bit hacky need to change from items control to a list box with selection</remarks>
    public class SetActiveLinkBehavior : Behavior<ItemsControl>
    {
        public static readonly DependencyProperty NavigationContainerProperty =
           DependencyProperty.Register("NavigationContainer", typeof(INavigationContainer), 
           typeof(SetActiveLinkBehavior), new PropertyMetadata(Utilities.Compatability.DP_UNSET_VALUE, OnNavigationContainerChanged));

        SiteMapNode _lastActiveNode;

        public INavigationContainer NavigationContainer
        {
            get { return (INavigationContainer)(GetValue(NavigationContainerProperty)); }
            set { SetValue(NavigationContainerProperty, value); }
        }

#region Overrides

        protected override void OnDetaching()
        {
            base.OnDetaching();
            if (this.NavigationContainer != null)
            {
                this.NavigationContainer.Navigated -= new System.EventHandler<NavigatedEventArgs>(NavigationContainer_Navigated);
            }
            _lastActiveNode = null;
        }

#endregion

#region Handlers

        private void NavigationContainer_Navigated(object sender, NavigatedEventArgs e)
        {
            foreach (NavigationNode _item in this.AssociatedObject.Items)
            {
                if (this.NavigationContainer.Url.StartsWith(_item.Url))
                {
                    if (_lastActiveNode != null) SetUnActive(_lastActiveNode);
                    SetActive(_item);
                    break;
                }
            }
        }

        private static void OnNavigationContainerChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            ((SetActiveLinkBehavior)d).SetupContainer((INavigationContainer)e.NewValue, (INavigationContainer)e.OldValue);
        }

#endregion

#region Helpers

        private void SetupContainer(INavigationContainer newContainer, INavigationContainer oldContainer)
        {
            if (oldContainer != null) oldContainer.Navigated -= NavigationContainer_Navigated;
            if (_lastActiveNode != null) SetUnActive(_lastActiveNode);

            if (newContainer != null) newContainer.Navigated += NavigationContainer_Navigated;
            _lastActiveNode = null;            
            SetActive((SiteMapNode)this.AssociatedObject.Items[0]);
        }

        private void SetActive(SiteMapNode siteMapNode)
        {
            var _container = (ContentPresenter)this.AssociatedObject.ItemContainerGenerator.ContainerFromItem(siteMapNode);
            VisualStateManager.GoToState(((Button)VisualTreeHelper.GetChild(_container, 0)), "ActiveLink", true);
            _lastActiveNode = siteMapNode;
        }

        private void SetUnActive(SiteMapNode siteMapNode)
        {
            var _container = (ContentPresenter)this.AssociatedObject.ItemContainerGenerator.ContainerFromItem(siteMapNode);
            VisualStateManager.GoToState(((Button)VisualTreeHelper.GetChild(_container, 0)), "InactiveLink", true);
        }

#endregion

    }
}
